package com.xe.core;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.text.SimpleTemplateEngine;
import groovy.text.Template;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.xe.Engine;
import com.xe.ResourceManager;


public class XEngine implements Engine{


	
	
	Binding binding = new Binding();
	GroovyShell shell = new GroovyShell(binding);
	SimpleTemplateEngine ste = new SimpleTemplateEngine();

	public XEngine() {
		this.setVariable(XEConstants.VAR_XE_ENGINE, this);
		this.setVariable(XEConstants.VAR_GROOVY_SHELL, shell);
		this.setVariable(XEConstants.VAR_GROOVY_TEMPLATE_ENGINE, ste);
	}

	
	@Override
	public Object run(String resourceName) {
		ResourceManager rm=(ResourceManager) binding.getProperty(XEConstants.VAR_XE_RESOURCE_MANAGER);
		XEResource resource = rm.load(resourceName);
		Object response = this.run(resource);
		return response;
	}

	@Override
	public Object run(XEResource resource) {
		return runAsGroovy(resource);
	}

	@Override
	public Object runScript(String script) {
		Object value = shell.evaluate(script);

		return value;
	}

	@Override
	public void setVariable(String name, Object value) {
		binding.setVariable(name, value);
	}

	
	public Object runAsGroovy (XEResource resource){

		Object value = shell.evaluate(resource.getSource());

		return value;
	}

	public void runAsGroovyTemplate (XEResource resource){
		Map<String,Object> bindingMap = binding.getVariables();
		HttpServletResponse res=(HttpServletResponse) bindingMap.get(XEConstants.VAR_HTTP_RESPONSE);
		Template t;
		try {
			t = ste.createTemplate(resource.getSource());
			t.make(bindingMap).writeTo(res.getWriter());
		} catch (Exception e){
			e.printStackTrace();
			try {
				e.printStackTrace(res.getWriter());
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
		
		

		
		
	}
	
	/*
	
	public void serveAsText (XEResource resource,HttpServletRequest req, HttpServletResponse res){
		
		try {
			Text source = resource.getSource();
			if(source!=null)
			res.getWriter().write(source.getValue());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public void serveAsBlob (XEResource resource,HttpServletRequest req, HttpServletResponse res){
			
		BlobKey blobKey = resource.getBlobKey();
		XEBlobstoreService bs = new XEBlobstoreService();
        //BlobInfoFactory bi = new BlobInfoFactory();
        //String fname = bi.loadBlobInfo(blobKey).getFilename();
        //res.setContentType("application/x-download");
        //res.setHeader("Content-Disposition", "attachment; filename=" + fname);
        
        bs.serve(blobKey, res);
		
	}
	
	
	
	*/

	
}
